package com.funtl.screw.codegen.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funtl.screw.codegen.entity.GenFormConf;
import com.funtl.screw.codegen.service.GenFormConfService;
import com.funtl.screw.common.core.util.QueryRequest;
import com.funtl.screw.common.core.util.ResponseResult;
import com.funtl.screw.common.log.annotation.SysLog;
import com.funtl.screw.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 表单管理
 *
 * @author Lusifer
 * @since 2023-08-12 15:55:35
 */
@RestController
@AllArgsConstructor
@RequestMapping("/form")
@Api(value = "form", tags = "表单管理")
public class GenFormConfController {

    private final GenFormConfService genRecordService;

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public ResponseResult<IPage<GenFormConf>> getGenFormConfPage(QueryRequest queryRequest, GenFormConf formConf) {
        Page<GenFormConf> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize());
        return ResponseResult.ok(genRecordService.page(page, Wrappers.query(formConf)));
    }

    /**
     * 通过id查询生成记录
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public ResponseResult<GenFormConf> getById(@PathVariable("id") Long id) {
        return ResponseResult.ok(genRecordService.getById(id));
    }

    /**
     * 通过id查询生成记录
     */
    @ApiOperation(value = "通过tableName查询表单信息")
    @Inner(value = false)
    @GetMapping("/info")
    public ResponseResult<String> form(String dsName, String tableName) {
        return ResponseResult.ok(genRecordService.getForm(dsName, tableName));
    }

    /**
     * 新增生成记录
     */
    @ApiOperation(value = "新增生成记录", notes = "新增生成记录")
    @SysLog("新增生成记录")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('gen_form_add')")
    public ResponseResult<Boolean> save(@RequestBody GenFormConf formConf) {
        formConf.setFormInfo(JSONUtil.toJsonPrettyStr(formConf.getFormInfo()));
        return ResponseResult.ok(genRecordService.save(formConf));
    }

    /**
     * 修改生成记录
     *
     * @param formConf 生成记录
     * @return R
     */
    @ApiOperation(value = "修改生成记录", notes = "修改生成记录")
    @SysLog("修改生成记录")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('gen_form_edit')")
    public ResponseResult<Boolean> updateById(@RequestBody GenFormConf formConf) {
        return ResponseResult.ok(genRecordService.updateById(formConf));
    }

    /**
     * 通过id删除生成记录
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除生成记录", notes = "通过id删除生成记录")
    @SysLog("通过id删除生成记录")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('gen_form_del')")
    public ResponseResult<Boolean> removeById(@PathVariable Long id) {
        return ResponseResult.ok(genRecordService.removeById(id));
    }

}
